package com.lc.luzhengtang.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.util.ActivityManager;
import com.lc.luzhengtang.weigh.DataCleanManager;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/4.
 */
public class SettingsActivity extends BaseActivity {
    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.layout_clearcache)
    LinearLayout layoutClearcache;
    @BindView(R.id.layout_change_pwd)
    LinearLayout layoutChangePwd;
    @BindView(R.id.layout_settingspay_pwd)
    LinearLayout layoutSettingspayPwd;
    @BindView(R.id.text_version_code)
    TextView textVersionCode;
    @BindView(R.id.layout_aboutus)
    LinearLayout layoutAboutus;
    @BindView(R.id.btn_logout)
    Button btnLogout;
    @BindView(R.id.text_huancun)
    TextView textHuancun;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.text_status)
    TextView textStatus;
    private String string_catchesize;

    private PopupWindow popWindow,popWindow2;

    UMAuthListener authListener = new UMAuthListener() {
        /**
         * @desc 授权开始的回调
         * @param platform 平台名称
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @desc 授权成功的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         * @param data 用户资料返回
         */
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
//            SocializeUtils.safeCloseDialog(dialog);
//            Toast.makeText(AuthActivity.this, "成功了", Toast.LENGTH_LONG).show();

        }

        /**
         * @desc 授权失败的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
//            SocializeUtils.safeCloseDialog(dialog);
//            Toast.makeText(AuthActivity.this, "失败：" + t.getMessage(), Toast.LENGTH_LONG).show();
        }

        /**
         * @desc 授权取消的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         */
        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
//            SocializeUtils.safeCloseDialog(dialog);
//            Toast.makeText(AuthActivity.this, "取消了", Toast.LENGTH_LONG).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        ButterKnife.bind(this);
        textTitle.setText("设置");
        try {
            string_catchesize = DataCleanManager.getTotalCacheSize(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d(string_catchesize, "++++++++++++++++++++++");
        textHuancun.setText(string_catchesize);

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(BaseApplication.basePreferences.readFlag())) {
            textStatus.setText("设置支付密码");
        } else {
            textStatus.setText("修改支付密码");
        }
    }

    @OnClick({R.id.img_news, R.id.layout_clearcache, R.id.layout_change_pwd, R.id.layout_settingspay_pwd, R.id.text_version_code, R.id.layout_aboutus, R.id.btn_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.layout_clearcache:
                showPopupWindow2(layoutClearcache);


                break;
            case R.id.layout_change_pwd:
                startActivity(new Intent(SettingsActivity.this, ChangePasswordActivity.class));

                break;
            case R.id.layout_settingspay_pwd:
                startActivity(new Intent(SettingsActivity.this, SettingsPasswordActivity.class));

                break;
            case R.id.text_version_code:
                break;
            case R.id.layout_aboutus:
//                startActivity(new Intent(SettingsActivity.this, AboutUsActivity.class));

                break;
            case R.id.btn_logout:
                showPopupWindow(btnLogout);
                break;
        }
    }


    private void showPopupWindow(View parent) {
        if (popWindow == null) {
    /*        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(this)
                    .inflate(R.layout.dialog_01, null)));*/
//            ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) convertView);
            View view = ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) LayoutInflater.from(this).inflate(R.layout.dialog_01, null));

            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop(view);
        }
        //设置动画效果
        popWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public void initPop(View view) {
        TextView tv01 = (TextView) view.findViewById(R.id.dl_tv_01);//拍照
        TextView tv02 = (TextView) view.findViewById(R.id.dl_tv_02);//相册

        tv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {


                popWindow.dismiss();

            }
        });
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                BaseApplication.basePreferences.clear();
                ActivityManager.getActivityManager().popAllActivity();



                UMShareAPI.get(SettingsActivity.this).deleteOauth(SettingsActivity.this, SHARE_MEDIA.WEIXIN, authListener);
//                UMShareAPI.get(AuthActivity.this).doOauthVerify(AuthActivity.this, SHARE_MEDIA.WEIXIN,authListener);
                finish();
                popWindow.dismiss();
            }
        });
    }




    private void showPopupWindow2(View parent) {
        if (popWindow2 == null) {
    /*        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(this)
                    .inflate(R.layout.dialog_01, null)));*/
//            ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) convertView);
            View view = ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) LayoutInflater.from(this).inflate(R.layout.dialog_clear_cache, null));

            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow2 = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop2(view);
        }
        //设置动画效果
        popWindow2.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow2.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow2.setOutsideTouchable(true);
        popWindow2.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow2.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow2.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public void initPop2(View view) {
        TextView tv01 = (TextView) view.findViewById(R.id.dl_tv_01);//拍照
        TextView tv02 = (TextView) view.findViewById(R.id.dl_tv_02);//相册

        tv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {


                popWindow2.dismiss();

            }
        });
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                DataCleanManager.clearAllCache(context);
                Toast.makeText(context, "清除缓存成功", Toast.LENGTH_SHORT).show();
                textHuancun.setText("0K");
                popWindow2.dismiss();
            }
        });
    }
}
